package com.luck.easyfoodbatch.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.luck.easyfoodbatch.entity.Place;
import com.luck.easyfoodbatch.mvp.model.Country;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lmw
 * @ClassName: FindDB
 * @Description: 数据库查询类
 * @date 2014-12-2 下午1:42:15
 */
public class PlaceDB {
    private static SQLiteDatabase database;
    private DBManager dBManager;

    public PlaceDB(Context context) {
        dBManager = new DBManager(context);

    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 打开数据库
     * @author lmw
     */
    protected void openDataBase() {
        database = dBManager.openDatabase();
    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 关闭数据库
     * @author lmw
     */
    protected void closeDataBase() {
        if (null != database) {
            database.close();
        }
    }


    /**
     * 得到所有分类
     *
     * @return
     */
    public List<Place> getByAllPlaceList() {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT * from place", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                List<Place> list = new ArrayList<Place>(num);
                if (cur.moveToFirst()) {
                    do {
                        String pid = cur.getString(cur.getColumnIndex("pid"));
                        String name = cur.getString(cur.getColumnIndex("name"));
                        Place place = new Place();
                        place.setPid(pid);
                        place.setName(name);
                        list.add(place);
                    } while (cur.moveToNext());
                }
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.close();
            cur.close();
        }
        return null;
    }

    /**
     * 查询指定数据
     *
     * @return
     */
    public List<Place> getByNamePlaceList(String pids) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT * from place where pid in (" + pids + ")", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                List<Place> list = new ArrayList<Place>(num);
                if (cur.moveToFirst()) {
                    do {
                        String pid = cur.getString(cur.getColumnIndex("pid"));
                        String name = cur.getString(cur.getColumnIndex("name"));
                        Place place = new Place();
                        place.setPid(pid);
                        place.setName(name);
                        list.add(place);
                    } while (cur.moveToNext());
                }
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.close();
            cur.close();
        }
        return null;
    }

    /**
     * 根据城市名查询相应的id
     */

    public String selectNameById(String cityName) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database.rawQuery("SELECT * from place where name ='"
                + cityName + "'", null);
        try {

            if (cur != null) {
                int num = cur.getCount();
                if (cur.moveToFirst()) {
                    do {
                        String pid = cur.getString(cur.getColumnIndex("pid"));
                        return pid;
                    } while (cur.moveToNext());
                }

                return "";
            }
        } catch (Exception e) {

        } finally {
            cur.close();
        }
        return "";
    }


    /**
     * 批量保存消息
     *
     * @return
     */
    public boolean savePlaceList(List<Country.DataBean> list) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        for (Country.DataBean model : list) {
            if (!isPlaceModel(model.getId())) {
                if ((null == database) || !database.isOpen()) {
                    openDataBase();
                }
                ContentValues values = new ContentValues();
                values.put("pid", model.getId());
                values.put("name", model.getName());
                database.insert("place", null, values);
            }
        }
        database.close();
        return true;
    }


    /**
     * 判断消息是否存在
     *
     * @return
     */
    public boolean isPlaceModel(String pid) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        StringBuffer buffer = null;
        Cursor cur = database
                .rawQuery("SELECT * from place where pid=" + pid, null);
        try {
            if (cur != null) {
                if (cur.moveToFirst()) {
                    do {
                        return true;
                    } while (cur.moveToNext());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //database.close();
            cur.close();
        }
        return false;
    }


    /**
     * @return void 返回类型
     * @Title: deleteDataBase
     * @Description: 删除数据库
     * @author lmw
     */
    public void deleteDataBase(Context context) {
        context.deleteDatabase("area.db");
    }

    /**
     * @return void 返回类型
     * @Title: deleteDataBase
     * @Description: 清空表
     * @author lmw
     */
    public void deleteTable() {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        database.execSQL("DELETE FROM place");
        database.close();
    }
}
